package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.CountrySiftingActivity;
import com.da.lon.wang.xlg.activity.ResultSearchActivity;
import com.da.lon.wang.xlg.bean.BrandResultBean.Brand;
import com.da.lon.wang.xlg.bean.BrandResultBean.BrandBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.util.PreferenceUtils;

import java.util.ArrayList;

public class ClassyBrandAdapter extends BaseAdapter implements OnClickListener {

    ArrayList<BrandBean> datas;
    Context context;
    LayoutInflater inflater;
    ClassyBrandItemAdapter adapter;

    public ClassyBrandAdapter(ArrayList<BrandBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas != null ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder1 holder = new Holder1();
        try {
            convertView = inflater.inflate(R.layout.classy_brand_item, null);
            holder.brand_type = (RelativeLayout) convertView
                    .findViewById(R.id.brand_type);
            holder.brand_type_text = (TextView) convertView
                    .findViewById(R.id.brand_type_text);
            holder.classy_brand_item_grid = (GridView) convertView
                    .findViewById(R.id.classy_brand_item_grid);
            if (position < datas.size()) {
                holder.brand_type.setOnClickListener(this);
                holder.brand_type.setTag(position);
                holder.brand_type_text.setText(datas.get(position).getName());
                final BrandBean bean = datas.get(position);
                if (bean.getBrand() != null) {
                    adapter = new ClassyBrandItemAdapter(bean.getBrand(),
                            context);
                    holder.classy_brand_item_grid.setAdapter(adapter);
                    setListViewHeightBasedOnChildren(holder.classy_brand_item_grid);
                    holder.classy_brand_item_grid
                            .setOnItemClickListener(new OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> parent,
                                                        View view, int position, long id) {
                                    if (!MyApplication
                                            .isNetworkAvailable(context)) {
                                        return;
                                    }
                                    String name = bean.getBrand().get(position)
                                            .getName();
                                    Intent intent = new Intent(context,
                                            ResultSearchActivity.class);
                                    Bundle bundle = new Bundle();
                                    PreferenceUtils
                                            .getInstance()
                                            .SetSettingString(
                                                    PreferencesConfig.SEARCH_SATTR,
                                                    "品牌_" + name);
                                    bundle.putString("cid", null);
                                    intent.putExtras(bundle);
                                    context.startActivity(intent);
                                }
                            });
                }
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return convertView;
    }

    /**
     * 动态获取gridView的总高度
     *
     * @param listView girdView对象
     */
    public void setListViewHeightBasedOnChildren(GridView listView) {
        // 获取listview的adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        // 固定列宽，有多少列
        int col = 4;// listView.getNumColumns();
        int totalHeight = 0;
        // i每次加4，相当于listAdapter.getCount()小于等于4时 循环一次，计算一次item的高度，
        // listAdapter.getCount()小于等于8时计算两次高度相加
        for (int i = 0; i < listAdapter.getCount(); i += col) {
            // 获取listview的每一个item
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            // 获取item的高度和
            totalHeight += listItem.getMeasuredHeight();
        }

        // 获取listview的布局参数
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        // 设置高度
        params.height = totalHeight;
        // 设置margin
        ((MarginLayoutParams) params).setMargins(7, 7, 7, 7);
        // 设置参数
        listView.setLayoutParams(params);
    }

    @Override
    public void onClick(View v) {
        if (!MyApplication.isNetworkAvailable(context)) {
            return;
        }
        int position = (int) v.getTag();
        Intent intent = new Intent(context, CountrySiftingActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("name", datas.get(position).getName());
        bundle.putString("cid", datas.get(position).getId());
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    class Holder1 {
        RelativeLayout brand_type;
        TextView brand_type_text;
        GridView classy_brand_item_grid;
    }

}

class ClassyBrandItemAdapter extends BaseAdapter {
    ArrayList<Brand> brand;
    Context context;
    LayoutInflater inflater;

    public ClassyBrandItemAdapter(ArrayList<Brand> brand, Context context) {
        this.brand = brand;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return brand != null ? brand.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return brand.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        try {
            convertView = inflater.inflate(R.layout.classy_brand_item_item,
                    null);
            holder.img = (ImageView) convertView
                    .findViewById(R.id.brand_item_img);
            if (position < brand.size()) {
                String path = brand.get(position).getImgurl();
                if (path != null && !"".equals(path)) {
//					MyApplication.loadImage(context, path, holder.img);
                    MyApplication.loadImageByPicasso(context, path, null, holder.img, 0, 0, 1);
                }
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return convertView;
    }

    class Holder {
        ImageView img;
    }
}
